package com.forum.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.forum.common.model.entity.Like;
import org.apache.ibatis.annotations.*;

@Mapper
public interface LikeMapper extends BaseMapper<Like> {
    
    /**
     * 查询用户是否已点赞
     * @param userId 用户ID
     * @param targetId 目标ID
     * @param type 类型 1-帖子 2-评论
     * @return 点赞记录
     */
    @Select("SELECT * FROM likes WHERE user_id = #{userId} AND target_id = #{targetId} AND type = #{type}")
    Like findByUserIdAndTargetIdAndType(@Param("userId") Long userId, @Param("targetId") Long targetId, @Param("type") Integer type);
    
    /**
     * 删除点赞记录
     * @param userId 用户ID
     * @param targetId 目标ID
     * @param type 类型 1-帖子 2-评论
     * @return 影响行数
     */
    @Delete("DELETE FROM likes WHERE user_id = #{userId} AND target_id = #{targetId} AND type = #{type}")
    int deleteByUserIdAndTargetIdAndType(@Param("userId") Long userId, @Param("targetId") Long targetId, @Param("type") Integer type);
    
    /**
     * 获取目标点赞数量
     * @param targetId 目标ID
     * @param type 类型 1-帖子 2-评论
     * @return 点赞数量
     */
    @Select("SELECT COUNT(*) FROM likes WHERE target_id = #{targetId} AND type = #{type}")
    int countByTargetIdAndType(@Param("targetId") Long targetId, @Param("type") Integer type);
    
    /**
     * 判断用户是否已点赞
     * @param userId 用户ID
     * @param targetId 目标ID
     * @param type 类型 1-帖子 2-评论
     * @return 是否已点赞
     */
    @Select("SELECT COUNT(*) FROM likes WHERE user_id = #{userId} AND target_id = #{targetId} AND type = #{type}")
    int existsByUserIdAndTargetIdAndType(@Param("userId") Long userId, @Param("targetId") Long targetId, @Param("type") Integer type);
} 